import api from '@/services/order/group-list';

const formatList = (list: OrderGroupListApi.ListResponse[]) => {
  const dataSource: OrderGroupListApi.TableItemInfo[] = [];
  for (let i = 0; i < list.length; i++) {
    const item = list[i];
    const { orderItemDatas, ...data } = item;
    // 一个订单里至少有一个商品
    if (orderItemDatas && orderItemDatas.length) {
      for (let j = 0; j < orderItemDatas.length; j++) {
        const orderItem = orderItemDatas[j];
        const result = { ...data, ...orderItem, rowSpan: j === 0 ? orderItemDatas.length : 0 };
        dataSource.push(result);
      }
    }
  }
  return dataSource;
};

export type OrderGroupListList = OrderGroupListApi.ListResponse;
export const getFilterList = async () => {
  const res = await api.getFilterList();
  return res.data;
};
export const getList = async (params: OrderGroupListApi.ListParams) => {
  const res = await api.getList<OrderGroupListList>(params);
  const tableData: Requestor.TableFormatedData<OrderGroupListApi.TableItemInfo> = {
    data: formatList(res.listData),
    current: res.page + 1,
    pageSize: res.limit,
    total: res.count,
    success: res.success,
  };

  return tableData;
};

export default {
  getFilterList,
  getList,
};
